package com.ruoyi.web.controller.biz;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.biz.domain.DormitoryBaseInfo;
import com.ruoyi.biz.service.DormitoryBaseInfoService;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 宿舍基础信息Controller
 *
 * @author alex
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/biz/dormitory")
public class DormitoryBaseInfoController extends BaseController {
    @Autowired
    private DormitoryBaseInfoService dormitoryBaseInfoService;

    /**
     * 查询宿舍基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:dormitory:list')")
    @GetMapping("/list")
    public TableDataInfo list(DormitoryBaseInfo dormitoryBaseInfo) {
        if (StringUtils.isNotEmpty(dormitoryBaseInfo.getDormitoryStatues())) {
            List<String> dormitoryStatusList = Arrays.stream(dormitoryBaseInfo.getDormitoryStatues().split(",")).collect(Collectors.toList());
            dormitoryBaseInfo.setDormitoryStatusList(dormitoryStatusList);
        } else {
            startPage();
        }
        List<DormitoryBaseInfo> list = dormitoryBaseInfoService.selectDormitoryBaseInfoList(dormitoryBaseInfo);
        return getDataTable(list);
    }

    /**
     * 导出宿舍基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:dormitory:export')")
    @Log(title = "宿舍基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DormitoryBaseInfo dormitoryBaseInfo) {
        List<DormitoryBaseInfo> list = dormitoryBaseInfoService.selectDormitoryBaseInfoList(dormitoryBaseInfo);
        ExcelUtil<DormitoryBaseInfo> util = new ExcelUtil<DormitoryBaseInfo>(DormitoryBaseInfo.class);
        util.exportExcel(response, list, "宿舍基础信息数据");
    }

    /**
     * 获取宿舍基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:dormitory:query')")
    @GetMapping(value = "/{dormitoryId}")
    public AjaxResult getInfo(@PathVariable("dormitoryId") Long dormitoryId) {
        return success(dormitoryBaseInfoService.selectDormitoryBaseInfoByDormitoryId(dormitoryId));
    }

    /**
     * 新增宿舍基础信息
     */
    @PreAuthorize("@ss.hasPermi('biz:dormitory:add')")
    @Log(title = "宿舍基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DormitoryBaseInfo dormitoryBaseInfo) {
        return toAjax(dormitoryBaseInfoService.insertDormitoryBaseInfo(dormitoryBaseInfo));
    }

    /**
     * 修改宿舍基础信息
     */
    @PreAuthorize("@ss.hasPermi('biz:dormitory:edit')")
    @Log(title = "宿舍基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DormitoryBaseInfo dormitoryBaseInfo) {
        return toAjax(dormitoryBaseInfoService.updateDormitoryBaseInfo(dormitoryBaseInfo));
    }

    /**
     * 删除宿舍基础信息
     */
    @PreAuthorize("@ss.hasPermi('biz:dormitory:remove')")
    @Log(title = "宿舍基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dormitoryIds}")
    public AjaxResult remove(@PathVariable Long[] dormitoryIds) {
        return toAjax(dormitoryBaseInfoService.deleteDormitoryBaseInfoByDormitoryIds(dormitoryIds));
    }
}